import riderOrderItem from '../../components/rider-order-item/rider-order-item.vue'
import riderDrawerCont from '../../components/rider-drawer-cont/rider-drawer-cont.vue'
import riderCommonPopup from '../../components/rider-common-popup/rider-common-popup.vue'
import { onMounted, ref, watch,provide, nextTick } from "vue";
import { db_riderQueryOrderList, db_submitSelfOrder } from '../../db/order-manage';
import { onShow } from '@dcloudio/uni-app';
import { buildTabListModule, orderTypeRule, removeTableListIndexData, scanCodeChangeOrder } from '../../utils';
import { buildFullBottomPopup } from '../../utils/common-popup';
import { runOrderCodeConvertView } from '../../utils/status-toggle';
import { objectAny } from '../../../options/base';
import riderTurnOrder from '../../components/rider-turn-order/rider-turn-order.vue'
import { timeOut } from '../../../utils/utils';

var statusBarHeight:number|undefined|0 =0;
// #ifdef  MP
statusBarHeight=uni.getSystemInfoSync().statusBarHeight
// #endif

export default {
    name:`rider_home_component`,
    components:{
        riderOrderItem,
        riderDrawerCont,
        riderCommonPopup,
        riderTurnOrder
    },
    setup() {
        const bottomPopupArgs = buildFullBottomPopup();
        for(let key in bottomPopupArgs){
            //@ts-ignore
            provide(key,bottomPopupArgs[key])
        }
        provide(`parentReload`,refreshList)

        // 左侧抽屉栏ref
        const riderDrawerContRef = ref<any>(null)
        const uniDrawerRef = ref<any>(null)
        const headTabIndex = ref<orderTypeRule>(0);
        const onlineStatus = ref({
            value:1,
            list:[{
                text: `在线中`,
				value: 1,
            },{
                text: `下线中`,
				value: 2,
            }]
        })
        const tabListModule = ref(buildTabListModule([
            (tableIndex,dbId)=>{
                removeTableListIndexData(tabListModule.value[tableIndex],dbId)
                return Promise.all([
                    refreshList(),
                    refreshList(1)
                ]);
            },
            (tableIndex,dbId)=>{
                removeTableListIndexData(tabListModule.value[tableIndex],dbId)
                return Promise.all([
                    refreshList(),
                    refreshList(2)
                ]);
            },
            (tableIndex,dbId)=>{
                removeTableListIndexData(tabListModule.value[tableIndex],dbId)
                return refreshList()
            }
        ]));
        onMounted(async ()=>{
            const index = 0;
            const orderType = tabListModule.value[index].value
            headTabIndex.value=orderType
        })
        onShow(()=>{
            refreshList();
        })
        watch(headTabIndex,async (value)=>{
            // 扫码转单
            if(value===4){
                bottomPopupArgs.turnOrderCreateQrcode();
            }else{
                refreshList();
            }
        },{
            immediate:true
        })

        // 刷新列表
        async function refreshList(index?:number){
            if(index == null){
                index = headTabIndex.value;
            }
            const currentModule=tabListModule.value[index] || {};
            try {
                if(currentModule.loadStatus===`pending`){
                    return console.log(`==========> 当前菜单正在获取数据中，跳过本次加载 <============`)
                }
                currentModule.loadStatus=`pending`;
    
                const orderType:orderTypeRule = currentModule.value
                const list = await db_riderQueryOrderList(orderType)

                currentModule.list = list.map((it:objectAny)=>{
                    runOrderCodeConvertView(it);
                    return it
                })
                currentModule.loadStatus=`fulfilled`;
            } catch (error) {
                console.error(error)
                currentModule.loadStatus=`fulfilled`;
            }
        }

        async function showDrawer() {
            uniDrawerRef.value.open();
            await nextTick()
            riderDrawerContRef.value.comOnShow();
        }
        function userStatusChange(e:any){
            console.log(e)
        }

        function selectTab(index:orderTypeRule){
            headTabIndex.value=index
        }

        return {
            riderDrawerContRef,
            tabListModule,
            onlineStatus,
            headTabIndex,
            uniDrawerRef,
            statusBarHeight,
            refreshList,
            showDrawer,
            selectTab,
            userStatusChange,
            scanCodeChangeOrder,

            ...bottomPopupArgs,
        }
    },
}